package com.googlecode.microanalyst.palette.figure;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.swt.graphics.Color;

import com.googlecode.microanalyst.designer.figures.ConnectedFigure;
import com.googlecode.microanalyst.palette.model.Dataset;

public class DatasetFigure extends ConnectedFigure {

	/** The inner TextFlow * */
	private TextFlow textFlow;

	final static Color COLOR_FRAME = new Color(
			ColorConstants.tooltipBackground.getDevice(), 64, 64, 64);

	final static Color COLOR_FILL = new Color(
			ColorConstants.tooltipBackground.getDevice(), 255, 204, 204);
	

	public DatasetFigure() {
		super();
		//connection anchor
		ConnectionAnchor outputAnchor = new ChopboxAnchor(this);
		outputConnectionAnchors.add(outputAnchor);
		connectionAnchors.put(Dataset.OUTPUT, outputAnchor);

		setBackgroundColor(ColorConstants.tooltipBackground);
		setForegroundColor(ColorConstants.tooltipForeground);

		FlowLayout centerLayout = new FlowLayout();
		centerLayout.setMajorAlignment(FlowLayout.ALIGN_CENTER);
		FlowPage flowPage = new FlowPage();
		flowPage.getInsets().top = 2;
		flowPage.getInsets().bottom = 3;
		flowPage.getInsets().left = 3;
		flowPage.getInsets().right = 3;

		textFlow = new TextFlow();

		textFlow.setLayoutManager(new ParagraphTextLayout(textFlow,
				ParagraphTextLayout.WORD_WRAP_SOFT));

		flowPage.add(textFlow);
		setLayoutManager(centerLayout);
		add(flowPage);
	}

	@Override
	protected void paintFigure(Graphics graphics) {
		Rectangle rect = getBounds().getCopy();

		graphics.translate(getLocation());

		//main rect
		graphics.setBackgroundColor(COLOR_FILL);
		graphics.fillRectangle(0, 0, rect.width, rect.height);
		graphics.setBackgroundColor(COLOR_FRAME);
		graphics.drawRectangle(0, 0, rect.width - 1, rect.height - 1);
		
		graphics.drawImage(Images.DATASET_IMAGE, 1, 1);
		
		//connection end point
		int h2 = rect.height / 2 - 1;
		graphics.fillRectangle(rect.width - 3, h2, 3, 3);

		graphics.translate(getLocation().getNegated());
	}

	/**
	 * Returns the text inside the TextFlow.
	 * 
	 * @return the text flow inside the text.
	 */
	public String getText() {
		return textFlow.getText();
	}

	/**
	 * Sets the text of the TextFlow to the given value.
	 * 
	 * @param newText
	 *            the new text value.
	 */
	public void setText(String newText) {
		textFlow.setText(newText);
	}


}
